package com.jic.member.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MememberGrowthBase;
import com.jic.member.request.MemberGrowthBaseRequest;
import com.jic.member.response.MememberGrowthBaseResponse;
import com.jic.member.service.MememberGrowthBaseService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("memberGrowth")
@Slf4j
public class MemberGrowthBaseController {
    @Autowired
    private MememberGrowthBaseService mememberGrowthBaseService;
    @ApiOperation("页面查询")
    @RequestMapping("getMemberGrowthMax")
    public RestResult<PageResult<MememberGrowthBaseResponse>> getMemberGrowthMax(@RequestBody MemberGrowthBaseRequest memberGrowthBaseRequest){
        return mememberGrowthBaseService.getGrowthInfo(memberGrowthBaseRequest);
    }

    @ApiOperation("修改成长值上限")
    @RequestMapping("saveGrowthMaxInfo")
    public RestResult<Long> saveGrowthMaxInfo(@RequestBody MemberGrowthBaseRequest memberGrowthBaseRequest){
         return mememberGrowthBaseService.update(memberGrowthBaseRequest);
    }

    @ApiOperation("查询组织层级")
    @RequestMapping("getDeptType")
    public MememberGrowthBaseResponse getDeptType(@RequestBody MemberGrowthBaseRequest memberGrowthBaseRequest){
        return mememberGrowthBaseService.getDeptType(memberGrowthBaseRequest);
    }

    /*
     * 分页查询
     * */
    @ApiOperation(value = "分页查询所有列表")
    @ResponseBody
    @RequestMapping("/queryGrowthBaseRuleList")
    public RestResult<PageResult<MememberGrowthBaseResponse>> queryGrowthBaseRuleList(@RequestBody MemberGrowthBaseRequest form) {
        log.info("开始进入分页查询所有列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MememberGrowthBase memberRequest = BeanUtil.copy(form, MememberGrowthBase.class);
        PageResult<MememberGrowthBase> result = mememberGrowthBaseService.queryPage(memberRequest, page);
        List<MememberGrowthBaseResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MememberGrowthBaseResponse memberResponse = BeanUtil.copy(temp, MememberGrowthBaseResponse.class);
            return memberResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<MememberGrowthBaseResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

}
